package 
{
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.geom.Rectangle;
    
    import ranger.RangerGame;
    
    import starling.core.Starling;

	[SWF(width="640", height="480", frameRate="60", backgroundColor="#565656")]
    public class RussellStarling extends Sprite
    {
		public static var WIDTH:Number = 640;
		public static var HEIGHT:Number = 480;
		
        private var _starling:Starling;
        private var _fLocalTest:Boolean;   // true when running locally

        public function RussellStarling()
        {
            if (stage)
            {
				// top-left and no-scale for Starling
				stage.align = StageAlign.TOP_LEFT; 
				stage.scaleMode = StageScaleMode.NO_SCALE;
				
                // if we have a stage then we're running locally.
                _fLocalTest = true;
            }

	        _starling = new Starling(RangerGame, stage);
	        _starling.showStats = true;
	        _starling.antiAliasing = 1;	// higher the better quality but slower performance
	        _starling.simulateMultitouch = true;
	        _starling.start();

	        stage.addEventListener(Event.RESIZE, onResize, false, int.MAX_VALUE, true);
	        stage.addEventListener(Event.DEACTIVATE, onDeactivate, false, 0, true);
        }

		private function onResize(event:Event):void
		{
			_starling.stage.stageWidth = stage.stageWidth;
			_starling.stage.stageHeight = stage.stageHeight;
			
			const viewPort:Rectangle = _starling.viewPort;
			viewPort.width = stage.stageWidth;
			viewPort.height = stage.stageHeight;
			try
			{
				_starling.viewPort = viewPort;
			}
			catch(error:Error) {}
		}
		
		private function onDeactivate(event:Event):void
		{
			_starling.stop();
			stage.addEventListener(Event.ACTIVATE, onActivate, false, 0, true);
		}
		
		private function onActivate(event:Event):void
		{
			stage.removeEventListener(Event.ACTIVATE, onActivate);
			_starling.start();
		}
		
        public function get fLocalTest():Boolean
        {
            return _fLocalTest;
        }
    }
}